package amis

import "fiberAmisAdmin/config"

func TextControl(fieldsForm []map[string]interface{}, route string, perPage string) (map[string]interface{}, error) {
	data := map[string]interface{}{
		"type": "page",
		"body": []interface{}{
			map[string]interface{}{
				"type":         "crud",
				"source":       "$rows",
				"api":          config.GetServerConfigValue("app_url") + route + "?_action=getData",
				"quickSaveApi": "/amis/api/mock2/sample/bulkUpdate",
				"syncLocation": false,
				"defaultParams": map[string]interface{}{
					"perPage": perPage,
				},
				"headerToolbar": []interface{}{
					map[string]interface{}{
						"label":      "新增",
						"type":       "button",
						"actionType": "dialog",
						"icon":       "fa fa-plus",
						"size":       "sm",
						"dialog": map[string]interface{}{
							"title": "新增表单",
							"body": map[string]interface{}{
								"type": "form",
								"api":  config.GetServerConfigValue("app_url") + route,
								"body": generateControlFields(fieldsForm),
							},
						},
					},
					"bulkActions",
					"export-csv",
				},
				"bulkActions": []interface{}{
					map[string]interface{}{
						"label":       "批量删除",
						"actionType":  "ajax",
						"api":         "delete:/amis/api/mock2/sample/${ids|raw}",
						"confirmText": "确定要批量删除?",
					},
				},
			},
		},
	}

	return data, nil
}

// 通用循环生成表单字段的函数
func generateControlFields(fields []map[string]interface{}) []map[string]interface{} {
	var controlFields []map[string]interface{}
	for _, field := range fields {
		column := map[string]interface{}{}
		for key, value := range field {
			column[key] = value
		}
		controlFields = append(controlFields, column)
	}
	return controlFields
}
